package battleGame;

import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.color.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.ArrayList;
public class Renderer extends JFrame implements Runnable, MouseListener
{
	static final int TILE_SIZE = 40;
	static final int WINDOW_SIZE_X = 1040;
	static final int WINDOW_SIZE_Y = 720;
	
	Image background;
	Thread animator;
	World myWorld;
	boolean b_showGrid = true;
	
	public Renderer()
	{
		
		super("BattleGame");
		
		animator = new Thread(this);
		
		//load image background 
		myWorld = World.BuildWorld(WINDOW_SIZE_Y/TILE_SIZE, WINDOW_SIZE_X/TILE_SIZE);
		background = (Toolkit.getDefaultToolkit().getImage("img//Map2.jpg"));
		
		
		setSize(WINDOW_SIZE_X,WINDOW_SIZE_Y);
		addMouseListener(this);
		setVisible(true);
		System.out.println("set visible true");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
		animator.start();
	}
	
	Vector<Warrior> army1WarriorList;
	Vector<Warrior> army2WarriorList;
	
	public void paint(Graphics g)
	{
		//background.paintIcon(this, g, 0, 0);
		g.drawImage(background,0,0,this);
		//draw all soldiers from both platoons
		
		/*
		for(int i=0;i<myWorld.platoon1.length;i++){
			//El dibujante debe tomar en cuenta su posicion x,y
		g.drawImage(myWorld.platoon1[i].getIcon(Warrior.UPRIGHT),myWorld.platoon1[i].col*(WINDOW_SIZE_X/myWorld.numCols),myWorld.platoon1[i].row*(WINDOW_SIZE_Y/myWorld.numRows),this);
		g.drawImage(myWorld.platoon2[i].getIcon(Warrior.DOWN),myWorld.platoon2[i].col*(WINDOW_SIZE_X/myWorld.numCols),myWorld.platoon2[i].row*(WINDOW_SIZE_Y/myWorld.numRows),this);
		}
		*/
		//myWorld.updateWorld(); //updat all the game vars
		if( myWorld != null )
		{
			Vector< GameObject > renderVector = myWorld.gameLogic.getRendererList();
			renderVector.add( myWorld.flag );
			ArrayList<GameObject> drawList=Utils.quick_srt( renderVector );
			
			for(int i =0; i< drawList.size();i++)
			{
				g.drawImage(drawList.get(i).getIcon(),(int)drawList.get(i).position.x,(int)drawList.get(i).position.y,this);
			}
			
			/*
			for(int i=0; i<army1WarriorList.size();i++)
			{
				g.drawImage(army1WarriorList.elementAt(i).getIcon(),
						    (int)army1WarriorList.elementAt(i).position.x,
						    (int)army1WarriorList.elementAt(i).position.y,
						    this);
			}
			
			for(int i=0; i<army2WarriorList.size();i++)
			{
				
				g.drawImage(army2WarriorList.elementAt(i).getIcon(),(int)army2WarriorList.elementAt(i).position.x,(int)army2WarriorList.elementAt(i).position.y,this);
			}
			*/

			//Flag
/*			g.drawImage( myWorld.flag.getIcon(), 
						 (int)myWorld.flag.position.x,
						 (int)myWorld.flag.position.y,
						 this );
*/
			//drawGrid(g);		   
		    
		    for(int i =0; i< WINDOW_SIZE_X/TILE_SIZE;i++)
		    {
		    	for(int j=0;j< WINDOW_SIZE_Y/TILE_SIZE;j++)
		    	{
		    		g.setColor(Color.white);
		    		g.drawString(myWorld.GetTile( i, j ).label+"", i*(WINDOW_SIZE_X/myWorld.numCols), j*(WINDOW_SIZE_Y/myWorld.numRows)+TILE_SIZE/2);
			
		    	}
		   }
		    
		}
		
		
	}
	
	public void drawGrid(Graphics g)
	{
		g.setColor(Color.red);		
		for(int i =0; i< WINDOW_SIZE_Y/TILE_SIZE;i++)
		{
			g.drawLine(0, i*TILE_SIZE, WINDOW_SIZE_X, i*TILE_SIZE);
		}
		for(int j=0;j< WINDOW_SIZE_X/TILE_SIZE;j++)
		{
			g.drawLine(j*TILE_SIZE, 0, j*TILE_SIZE, WINDOW_SIZE_Y);		
		}
		
	}
	
	public void run(){
		while(true){
			
			try{
			Thread.sleep(100);
			myWorld.updateWorld();
			repaint();	
			}catch(Exception e)
			{
				
			}
		}
	}
	
	public void mousePressed(MouseEvent evt)
	{
		//System.out.println("mouse event");
		myWorld.gameLogic.update();
	}
	public void mouseReleased(MouseEvent evt)
	{
		
	}
	
	public void mouseClicked(MouseEvent evt){
		
	}
	public void mouseEntered(MouseEvent evt){
		
	}
    public void mouseExited(MouseEvent evt){
    	
    }
}
